﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;
using SYDZ.WMS.DBUtility;

namespace ElectronicWMSA.T_WMS.Basic
{
    public partial class MachineFPFrm : Form
    {
        private string strWhere1 = "";
        public MachineFPFrm()
        {
            InitializeComponent();
        }
        private void MachineFPFrm_Load(object sender, EventArgs e)
        {
            this.pageControl1.PageCurrent = 1;
            this.pageControl1.PageSize = 50;
            this.pageControl1.Bind();
        }


        #region 数据
        private int DBindData1()
        {
            Comm.Forms.MsControls.PageData pageData = new Comm.Forms.MsControls.PageData();
            pageData.TableName = "T_PackBoxMachineCodeList";
            pageData.PrimaryKey = "NID";
            pageData.OrderStr = "NID";
            pageData.PageIndex = this.pageControl1.PageCurrent;
            pageData.PageSize = this.pageControl1.PageSize;
            pageData.QueryCondition = strWhere1;
            pageData.QueryFieldName = "*";

            this.pageControl1.bindingSource.DataSource = pageData.QueryDataTable().Tables[0];
            this.pageControl1.bindingNavigator.BindingSource = pageControl1.bindingSource;
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = this.pageControl1.bindingSource;
            this.dataGridView1.ClearSelection();
            this.dataGridView1.CurrentCell = null;
            return pageData.TotalCount;
        }
        private int pageControl1_EventPaging(Comm.Forms.MsControls.EventPagingArg e)
        {
            return DBindData1();
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        #endregion

        private void btnUserSearch_Click(object sender, EventArgs e)
        {
            strWhere1 = " 1=1 ";
            if (!SystemVariable.VaildationPermission("查询")) return;
            string str1 = txt_01.Text.Trim();
            string str2 = txt_02.Text.Trim();
            if (!string.IsNullOrEmpty(str1))
            {
                strWhere1 += " and box_code='" + str1 + "'";
            }
            if (!string.IsNullOrEmpty(str2))
            {
                strWhere1 += " and machine_code='" + str2 + "'";
            }
            this.pageControl1.Bind();
        }

        private void btnNewUser_Click(object sender, EventArgs e)
        {
            this.btnUSave.Text = "添 加";
            this.tabAnalysisList.SelectedIndex = 1;
        }
        private void btnUSave_Click(object sender, EventArgs e)
        {
            string str1 = txt1.Text.Trim();
            string str2 = txt2.Text.Trim();
            string str3 = txt3.Text.Trim();
            string sql = "select NID from T_PackBoxMachineCodeList where box_code='" + str1 + "' AND machine_code='" + str2 + "'";
            DataSet dt1 = DbHelperSQL.Query(sql);
            if (dt1 != null && dt1.Tables[0].Rows.Count > 0)
            {
                MessageClass.warning("已存在不能重复，请确认！");
            }
            else
            {
                if (!ValidationRegex.ValidteData(str3))
                {
                    MessageClass.error("数量必须为整数，请确认！");
                    txt3.Focus();
                    return;
                }
                if (btnUSave.Text == "添 加")
                {
                    if (MessageClass.enquire("确认新增？") == DialogResult.OK)
                    {
                        int result = DbHelperSQL.ExecuteSql("insert into T_PackBoxMachineCodeList (box_code,machine_code,package_box_cnt)values('" + str1 + "','" + str2 + "','" + str3 + "') ");
                        if (result > 0)
                        {
                            pageControl1.Bind();
                            UpdateID = 0;
                            txt1.Text = "";
                            txt2.Text = "";
                            txt3.Text = "";
                            SystemVariable.AddLogInfo("追溯条码式样管理", "新增追溯条码式样管理信息。");
                            MessageClass.right("操作完成！");
                        }
                        else
                        {
                            MessageClass.error("操作失败！");
                        }
                    }
                }
                else
                {
                    if (MessageClass.enquire("确认编辑？") == DialogResult.OK)
                    {
                        int result = DbHelperSQL.ExecuteSql("update T_PackBoxMachineCodeList set box_code='" + str1 + "',machine_code='" + str2 + "',package_box_cnt='" + str3 + "' where nid='" + UpdateID + "' ");
                        if (result > 0)
                        {
                            UpdateID = 0;
                            txt1.Text = "";
                            txt2.Text = "";
                            txt3.Text = "";
                            pageControl1.Bind();
                            SystemVariable.AddLogInfo("追溯条码式样管理", "编辑追溯条码式样管理信息。");
                            MessageClass.right("操作完成！");
                            tabAnalysisList.SelectedIndex = 0;
                            btnUSave.Text = "添 加";
                        }
                        else
                        {
                            MessageClass.error("操作失败！");
                        }
                    }
                }
            }
        }
        private int UpdateID = 0;
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            UpdateID = 0;
            if (e.RowIndex > -1)
            {
                if (this.dataGridView1.Rows.Count != 0)
                {
                    #region 获取单列详细信息
                    if (e.ColumnIndex != -1 && dataGridView1.CurrentCell.ColumnIndex == 0)
                    {
                        UpdateID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["NID"].Value.ToString());
                        txt1.Text = dataGridView1.CurrentRow.Cells["box_code"].Value.ToString();
                        txt2.Text = dataGridView1.CurrentRow.Cells["machine_code"].Value.ToString();
                        txt3.Text = dataGridView1.CurrentRow.Cells["package_box_cnt"].Value.ToString();
                        btnUSave.Text = "修 改";
                        this.tabAnalysisList.SelectedIndex = 1;
                    }
                    else
                    {
                        UpdateID = 0;
                        txt1.Text = "";
                        txt2.Text = "";
                        txt3.Text = "";
                    }
                    #endregion

                    if (e.ColumnIndex != -1 && dataGridView1.CurrentCell.ColumnIndex == 1)
                    {
                        UpdateID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["NID"].Value.ToString());
                        if (UpdateID == 0)
                        {
                            MessageClass.error("请选择信息！"); return;
                        }
                        if (MessageClass.enquire("删除选择?") == DialogResult.OK)
                        {
                            string SelectSQL = " delete from T_PackBoxMachineCodeList where NID='" + UpdateID + "' ";
                            int result = DbHelperSQL.ExecuteSql(SelectSQL);
                            if (result > 0)
                            {
                                UpdateID = 0;
                                pageControl1.Bind();
                                SystemVariable.AddLogInfo("追溯条码式样管理", "删除追溯条码式样管理信息。");
                                MessageClass.right("操作完成！");
                            }
                            else
                            {
                                MessageClass.error("操作失败！");
                            }
                        }
                    }
                }
            }
        }
        private void btnUClear_Click(object sender, EventArgs e)
        {
            txt1.Text = "";
            txt2.Text = "";
            txt3.Text = "";
            UpdateID = 0;
        }
        private void btnRetunList_Click(object sender, EventArgs e)
        {
            tabAnalysisList.SelectedIndex = 0;
        }
    }
}
